<?php
/*
* IKPHP爱客网 安装程序 @copyright (c) 2012-3000 IKPHP All Rights Reserved @author 小麦
* @Email:ikphp@sina.cn
* @小麦 关注 2014年8月9日12:11:41
*/
namespace IKPHP\Apps\Space\Models;

use IKPHP\Common\Models\BaseModel;
use IKPHP\Common\Models\User;

class UserPhotoAlbum extends BaseModel {
	
	public $albumid;
	public $userid;
	public $albumname;
	public $count_photo;
	public $count_view;
	public $isrecommend;
	
	public function getSource() {
		return IK . "user_photo_album";
	}
    /* 用户模型自动完成 */	
    public function beforeCreate()
    {
    	$this->count_photo 	= 0;
        $this->count_view 	= 0;
        $this->isrecommend	= 0; 
        
    }
	//获取相册列表
	public static function getAlbums($map,$order='addtime DESC',$limit = '5')
	{
		$where = array('conditions'=>$map,'columns'=>'albumid', 'order'=>$order, 'limit'=> $limit);
		$res   = self::find($where); 
		if($res){
			foreach($res->toArray() as $key=>$item){
				$result[$key] = self::getOneAlbum($item['albumid']);
			}
			return $result;
		}else{
			return false;
		}
	}    	
	//获取一个相册
	public static function getOneAlbum($id){
		$where = array("albumid = '$id'");
		$result = self::findFirst($where);
		if($result){
			$strAlbum = $result = $result->toArray();
			if(!empty($result['path'])){
				//存在封面图片路径
				$ext =  explode ( '.', $result['albumface']);
				//图片大小
				$strAlbum['simg'] =  attach($result['path'].$ext[0].'_'.C('ik_simg.width').'_'.C('ik_simg.height').'.'.$ext[1]);
				$strAlbum['mimg'] =  attach($result['path'].$ext[0].'_'.C('ik_mimg.width').'_'.C('ik_mimg.height').'.'.$ext[1]);
			}else{
				$strAlbum['simg'] = $strAlbum['mimg'] = C('ik_site_url') . "static/images/photo_album.png";
			}
			return $strAlbum;
		}else{
			return false;
		}		
	}	
	//获取推荐相册
	public static function getRecommendAlbum($limit = 10){ 
		$where = array("isrecommend = 1",'columns'=>'albumid', 'order'=>'addtime ASC', 'limit'=> $limit);
	    $res = self::find($where);
		if($res){
			foreach($res->toArray() as $key=>$item){
				$result[$key] = self::getOneAlbum($item['albumid']);
			}
			return $result;
		}else{
			return false;
		}
	}					
}
